if not menu then menu = {} end

if not menu.portfolio then


dofile (getScriptPath() .. "\\..\\lib\\lib.lua")
dofile (getScriptPath() .. "\\..\\lib\\orders.lua")
dofile (getScriptPath() .. "\\..\\processor\\OnOrder.lua")
dofile (getScriptPath() .. "\\..\\processor\\OnStopOrder.lua")


menu.portfolio = {commission = 0.0015}


processor:AddInit(function()
	menu.portfolio.row = window:AddRow()
	
	menu.portfolio.firm_id = lib:GetFirmID()
end)


processor:AddHandler("OnTableNotification", function(data)
	if data.msg == 11 and data.par1 == menu.portfolio.row and data.par2 == 1 then
		lib:SelectTab(1)
	end
end)

processor:AddHandler("OnOrder", function(order)
	menu.portfolio.time = lib:tickseconds() + 1
end)


processor:AddHandler("OnStopOrder", function(order)
	menu.portfolio.time = lib:tickseconds() + 1
end)


processor:AddTimer(function()
	if not menu.portfolio.time or menu.portfolio.time < lib:tickseconds() then
		menu.portfolio.time = lib:tickseconds() + 3
		
		local a = lib:GetAssets()
		local m, l = lib:GetMoney()
		local pl, rc = lib:GetPL()
		local pos = orders:GetPosition(nil, true)
		
		if a and menu.portfolio.assets ~= a then
			menu.portfolio.assets = a
			window:SetCell(menu.portfolio.row, 1,
				lib:FormatBalance(menu.portfolio.assets)
			)
		end
		
		if menu.portfolio.money ~= m then
			menu.portfolio.money = m
			menu.portfolio.changed = true
		end
		
		if menu.portfolio.money_locked ~= l then
			menu.portfolio.money_locked = l
			menu.portfolio.changed = true
		end
		
		if lib:tostring(menu.portfolio.positions) ~= lib:tostring(pos) then
			menu.portfolio.positions = pos
			menu.portfolio.changed = true
		end
		
		if pl and menu.portfolio.assets and menu.portfolio.pl ~= pl then
			menu.portfolio.pl = pl
		end
		
		if rc and menu.portfolio.assets and menu.portfolio.rate_change ~= rc then
			menu.portfolio.rate_change = rc
			window:SetCell(menu.portfolio.row, 2,
				string.format("%.2f%%", rc)
			)
		end
	end
end)


end -- of file

